package com.situ.root.goodsB.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.situ.root.goodsB.model.GoodsBModel;
import com.situ.root.goodsB.service.IGoodsBService;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("goodsB")
public class GoodsBController {

	@Autowired
	private IGoodsBService goodsBService;
	
	@RequestMapping(value = "/add")
	@ResponseBody
	public String add(GoodsBModel userModel ,Model model) {
		int res = goodsBService.insert(userModel);
		System.out.println("res=" + res);
		model.addAttribute("user", userModel);
		model.addAttribute("msg", "注册成功");
		return "1";
	}
//	,method= {RequestMethod.GET,RequestMethod.POST}
	@RequestMapping(value = "/list", produces={"application/json;charset=UTF-8"},method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public List<GoodsBModel> list(GoodsBModel userModel,Model model) {
		List<GoodsBModel> res = goodsBService.selectList(userModel);
		res = JSONArray.fromObject(res);
		System.out.println("res=" + res);
		model.addAttribute("mumu",res);
		return res;
	}
	
	@RequestMapping(value = "/upd", produces={"application/json;charset=UTF-8"},method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String upd(GoodsBModel userModel) {
		int res = goodsBService.update(userModel);
		System.out.println("res="+res);
		return "1";
	}
	
	@RequestMapping(value = "/upds")
	@ResponseBody
	public String upds(GoodsBModel userModel) {
		int res = goodsBService.updates(userModel);
		System.out.println("res="+res);
		return "1";
	}
	
	@RequestMapping(value = "/get", produces={"application/json;charset=UTF-8"},method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public List<GoodsBModel> get(GoodsBModel userModel) {
		List<GoodsBModel> res = goodsBService.selectList(userModel);
		res = JSONArray.fromObject(res);
		System.out.println("res="+res);
		return res;
	}
	
	@RequestMapping(value = "/del",method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String del(GoodsBModel userModel,Object id) {
		int res = goodsBService.delete(userModel);
		System.out.println("res=" + res);
//		return "page/user/list";
		return "1";
	}
	
	@RequestMapping(value = "/page",method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Map<String, Object> page(GoodsBModel model,Model models) {
		model.setPageOn(true);
		Map<String,Object> map = new HashMap<>();
		map.put("data", goodsBService.selectList(model));
        map.put("count", goodsBService.selectCount(model));
		map = net.sf.json.JSONObject.fromObject(map);
		System.out.println("map=" + map);
		models.addAttribute("mumu",map);
		return map;
	}
	
}
